import urlUtils from '@/libs/urlutils';
import storage from '@/libs/storage';
import ubt from '@/service/ubt';
import utils from '@/libs/utils';

export default {
  getChannel() {
    const channel = storage.get('channel');
    return channel || urlUtils.getParams('channel') || 'qingjie000-llcs'; // 空的情况下默认值
  },

  setChannel() {
    const urlChannel = urlUtils.getParams('channel');

    // channel 存在替换掉本地，否则用原来的
    if (urlChannel) {
      storage.set('channel', urlChannel);
    }
  },

  beforeDestroy(store, router) {
    window.onbeforeunload = () => {
      let { name } = router.currentRoute;
      const startTime = store.state.app.startTime;

      if (name === 'login') {
        name = 'register';
      }

      const eventName = `${utils.fistLetterUpper(name)}.Stay`;

      ubt.eeLogUBT('stay', eventName, {
        pageStayTime: new Date().getTime() - startTime
      });
    };
  },

  isSJD() {
    const autoUserId = storage.get('autoUserId');
    const channel = this.getChannel();

    return autoUserId && (channel === 'qingjie-dl-sjd-app-h5-zjwt-llcs' || channel === 'qingjie-jj-sjd-app-h5-drjj-llcs');
  }
};
